//
//  SeePlanViewController.m
//  wangzhe
//
//  Created by 李 佳伟 on 2017/11/27.
//  Copyright © 2017年 李 佳伟. All rights reserved.
//

#import "SeePlanViewController.h"
#import "JTCSM.h"
#import "SeePlanModel.h"
#import "SeePlanTableViewCell.h"
@interface SeePlanViewController ()<UITableViewDelegate,UITableViewDataSource,UIAlertViewDelegate>
{
    UITableView *_tableview;
    NSMutableArray *titlearray;
    UIView *backView;
    UILabel *totalLabel;
    UILabel *repaylabel;
    UILabel *unpayLabel;
}


@end

@implementation SeePlanViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [df isRemoveTarbar:YES];
    self.view.backgroundColor = [UIColor whiteColor];
    leftBarButtonItems_rightBarButtonItems *leftbtn = KCUSTOMNAVIGSTION_ALLOC;
    self.navigationItem.leftBarButtonItem = [leftbtn setBarButtonItemsImageName:@"fanhui.png" frame:KLeftBarButtonItemFrame_image target:self SEL:@selector(gotBack)];
    
    self.navigationItem.rightBarButtonItem= [leftbtn setBarButtonItemsTitle:@"取消计划" frame:KRightBarButtonItemFrame_text target:self SEL:@selector(gotoMainV)];
    [self layoutTableView];
    [self theRequest];
}
- (void)gotBack{
    [self.navigationController popViewControllerAnimated:YES];
}

- (UIView *)layoutTableViewfooter{
    
    backView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, KCONTROLLERWith, 70)];
    backView.backgroundColor = [UIColor whiteColor];
    backView.userInteractionEnabled = YES;
    
    CGFloat btnspace = 40;
    CGFloat btnHeight = 50;
    UIButton *LoginBtn =[[UIButton alloc] initWithFrame:CGRectMake(btnspace, 20 , (KCONTROLLERWith-2*btnspace), btnHeight)];
    LoginBtn.backgroundColor =RGBACOLOR(227, 68, 40, 1);
    [LoginBtn setTitle:@"取消还款计划" forState:UIControlStateNormal];
    [LoginBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    //[df layerWithView:LoginBtn radius:4 width:2 color:RGBACOLOR(172,134,171, 1)];
    [LoginBtn addTarget:self action:@selector(gotoMainV) forControlEvents:UIControlEventTouchUpInside];
    LoginBtn.tag = 10002;
    [backView addSubview:LoginBtn];
    [df layerWithView:LoginBtn radius:btnHeight/2 width:0 color:nil];
    
    return backView;

}

#pragma mark    清除计划

- (void)gotoMainV{
//    CreditPlan/delete
    [df showAlertWithTitle:@"确定取消还款计划？" message:nil btn1:@"确定" btn2:@"取消" btn3:nil delegate:self tag:12];
    
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (alertView.tag == KAlertVTag+12) {
        LoadData *request = [[LoadData alloc]init];
        [request loadDataPostAsynchronousWithURL:KURLPOSTHeadr(@"CreditPlan/delete") withDict:@{@"token":KTokenString,@"card_id":self.card_id} superView:nil isAddtag:NO Block:^(id result) {
            
            NSString *isreturn = [NSString stringWithFormat:@"%@",[result objectForKey:@"status"]];
            if ([isreturn isEqualToString:@"1"]) {
                
                [self gotBack];
            }
        }];
    }
}
- (void)layoutTableView{
    
    _tableview = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, KCONTROLLERWith, KCONTROLLERHeight-50) style:UITableViewStyleGrouped];
    _tableview.delegate = self;
    _tableview.dataSource = self;
    _tableview.tableHeaderView = [self layoutheaderview];
    [self.view addSubview:_tableview];
    _tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
    _tableview.backgroundColor = [UIColor whiteColor];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return titlearray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    //
    SeePlanModel *model = titlearray[section];
    return model.plan.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NSString *cellid = [NSString stringWithFormat:@"iii%ld%ld",(long)indexPath.section,(long)indexPath.row];
    
    SeePlanTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellid];
    if (cell == nil) {
        cell = [[SeePlanTableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellid SeePlanTableViewCellType:isSeePlanType];
    }
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    SeePlanModel *model = titlearray[indexPath.section];
    if (indexPath == nil || indexPath.row>=model.plan.count) {
        return cell;
    }
    SeePlanDetailModel *detailmodel = [SeePlanDetailModel whc_ModelWithJson:model.plan[indexPath.row]];
    [cell setvalueWithSeePlanDetailModel:detailmodel];
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 60;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
     NSString *cellid = [NSString stringWithFormat:@"iii%ld",(long)section];
   SeePlanTableViewCell *cell= [[SeePlanTableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellid SeePlanTableViewCellType:isSeePlanType];
    [cell setvalueWithSeePlanModel:titlearray[section] SeePlanTableViewCellType:isSeePlanType];
    
    return cell;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return [SeePlanTableViewCell whc_CellHeightForIndexPath:indexPath tableView:tableView];
}

- (void)theRequest{
    [titlearray removeAllObjects];
    titlearray = nil;
    titlearray = [NSMutableArray array];
    
    LoadData *request = [[LoadData alloc]init];
    [request loadDataPostAsynchronousWithURL:KURLPOSTHeadr(@"CreditPlan/getList") withDict:@{@"token":KTokenString,@"card_id":self.card_id} superView:nil isAddtag:NO Block:^(id result) {
        
        NSString *isreturn = [NSString stringWithFormat:@"%@",[result objectForKey:@"status"]];
        if ([isreturn isEqualToString:@"1"]) {
            
//            "total_price":3000,"repay":0,"unpay":3000,
            totalLabel.text = [NSString stringWithFormat:@"%.2f",[[KResultOfKey(@"data") objectForKey:@"total_price"] floatValue]];
            repaylabel.text = [NSString stringWithFormat:@"已还金额：%.2f",[[KResultOfKey(@"data") objectForKey:@"repay"] floatValue]];
            unpayLabel.text = [NSString stringWithFormat:@"未还金额：%.2f",[[KResultOfKey(@"data") objectForKey:@"unpay"] floatValue]];
            
            NSArray *bannerArr = [KResultOfKey(@"data") objectForKey:@"list"];
            [titlearray removeAllObjects];
            for (NSDictionary *bannerdict in bannerArr) {
                SeePlanModel *model = [[SeePlanModel  alloc]init];
                [model setValuesForKeysWithDictionary:bannerdict];
                [titlearray addObject:model];
            }
            
            [_tableview reloadData];
        }
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(UIView *)layoutheaderview{
    
    UIView *backview = [[UIView alloc]initWithFrame:CGRectMake(0, 0, KCONTROLLERWith, 180)];
    backview.backgroundColor = RGBACOLOR(74, 135, 242, 1);
    UILabel *titlelabel = [UILabel initWithFrame:CGRectMake(0, 30, KCONTROLLERWith, 30) text:@"还款总金额" font:14 textAlignment:NSTextAlignmentCenter texcolor:RGBACOLOR(255, 255, 255, 0.6) backgroundColor:nil];
    [backview addSubview:titlelabel];
    
    totalLabel = [UILabel initWithFrame:CGRectMake(0, KVIEW_Y(titlelabel), KCONTROLLERWith, 30) text:@"还款总金额" font:25 textAlignment:NSTextAlignmentCenter texcolor:RGBACOLOR(255, 255, 255, 1) backgroundColor:nil];
    [backview addSubview:totalLabel];
    
    repaylabel = [UILabel initWithFrame:CGRectMake(0, 30+KVIEW_Y(totalLabel), KCONTROLLERWith/2, 30) text:@"还款总金额" font:14 textAlignment:NSTextAlignmentCenter texcolor:RGBACOLOR(255, 255, 255, 0.8) backgroundColor:nil];
    [backview addSubview:repaylabel];
    
    unpayLabel = [UILabel initWithFrame:CGRectMake(KCONTROLLERWith/2, 30+KVIEW_Y(totalLabel), KCONTROLLERWith/2, 30) text:@"还款总金额" font:14 textAlignment:NSTextAlignmentCenter texcolor:RGBACOLOR(255, 255, 255, 0.8) backgroundColor:nil];
    [backview addSubview:unpayLabel];
    
    
    return backview;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
